package yxy.game.pm2.module.therionHunting;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.theriondate;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;


import java.util.Arrays;
import java.util.Map;

/**
 * @author wrs
 * @name TherionModelInfo
 * @description 神兽玩法模式信息
 * @since 2022-06-13
 */
public class TherionModelInfo extends BaseGameRedisLockBean {

    // bean==========================================================================

    private int id;
    private int type; //1神兽争夺,2神兽猎场，3世界神兽，4神兽争夺青龙，5神兽争夺白虎，6神兽争夺朱雀，7神兽争夺玄武
    private int[] condition; //关卡开放时间：每周的第几天，周一为1；0为全天开放
    private int[] rewardtype; //奖励类型
    private int[] rewardid; //奖励ID
    private int[] rewardnum; //奖励数量

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int[] getCondition() {
        return condition;
    }

    public void setCondition(int[] condition) {
        this.condition = condition;
    }

    public int[] getRewardtype() {
        return rewardtype;
    }

    public void setRewardtype(int[] rewardtype) {
        this.rewardtype = rewardtype;
    }

    public int[] getRewardid() {
        return rewardid;
    }

    public void setRewardid(int[] rewardid) {
        this.rewardid = rewardid;
    }

    public int[] getRewardnum() {
        return rewardnum;
    }

    public void setRewardnum(int[] rewardnum) {
        this.rewardnum = rewardnum;
    }

    // func===========================================================================

    private static TherionModelInfo create(int type) {
        TherionModelInfo module = new TherionModelInfo();
        Map<String, theriondate> allData = DataCenter.getAllData(theriondate.class);
        for (theriondate theriondate : allData.values()) {
            if (theriondate.getType() == type){
                module.setId(theriondate.getId());
                module.setType(theriondate.getType());
                module.setRewardtype(theriondate.getRewardtype());
                module.setRewardid(theriondate.getRewardid());
                module.setRewardnum(theriondate.getRewardnum());

                String[] strings = theriondate.getCondition().split(",");
                int[] conditions = Arrays.stream(strings).mapToInt(Integer::parseInt).toArray();
                module.setCondition(conditions);
                module.set(theriondate.getType());
            }
        }
        return module;
    }

    // redis==========================================================================

    static public String key(int type) {
        return String.format("THERION:MODEL:%s:MODELINFO",type);
    }

    public void set(int type) {
        try {
            setv(key(type), this);
        }catch (RedisException e){
            e.printStackTrace();
        }
    }

    static public TherionModelInfo get(int type) {
        try {
            String key1 = key(type);
            TherionModelInfo module = getv(key1);
            //todo 后续缓存加到数据库

            if (module == null) {
                module = create(type);
            }
            return module;
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    //红点
    private void check() {

    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {

    }


    // lock==========================================================================

    static private String lockKey(int type) {
        return String.format("lock:%s", key(type));
    }

    static public boolean lockTherionModelInfo(int type,String playerCode) throws InterruptedException {
        return tryLock(lockKey(type));
    }

    static public void unlockTherionModelInfo(int type,String playerCode) {
        unlock(lockKey(type));
    }
}
